<template>
    <div class="listDiv pd10">
        <div class="searchDiv mb20">
            <div class="flex1 flex-row-center">
                <el-icon @click="proxy.$router.go(-1)" class="backicon"><Back /></el-icon>
                <span class="title ml10">{{ proxy.$route.query.name||'' }}</span>
            </div>
            <el-button type="primary" @click="add">上传知识</el-button>
        </div>
        <div class="w100 flex-row wrap overflow-y">
            <div v-for="(v,i) in obj.ary" :key="i" class="filecard flex-row">
                <el-icon style="font-size: 30px;margin-right: 10px;"><DocumentDelete /></el-icon>
                <div class="flex1 flex-column hidden">
                    <div class="flex-row-center mb10">
                        <el-tooltip
                            effect="dark"
                            :content="v.name"
                            placement="top-start"
                        >
                            <div class="name flex1 lineOne mr20">{{ v.name }}</div>
                        </el-tooltip>
                        <el-dropdown :ref="`dropdown${v.id}`" trigger="contextmenu">
                            <el-icon @click="showtab(v)"><More /></el-icon>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item @click="preview(v)">下载</el-dropdown-item>
                                    <el-dropdown-item @click="del(v)"><span style="color: red;">删除</span></el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </div>
                    <div class="flex-row-center mb5">
                        <span class="desc">{{ v.word_num }}字</span>
                        <span class="desc">知识类型：文章知识</span>
                    </div>
                    <div class="desc flex-row-center"><span class="lineOne" style="width: 150px;">文档ID:{{ v.id }}</span><el-icon class="pointer" @click="proxy.$util.copyText(v.id)"><DocumentCopy /></el-icon></div>
                </div>
            </div>
        </div>

        <el-drawer v-model="obj.drawer.show" :title="obj.drawer.title" size="50%" direction="rtl" :close-on-click-modal="false" destroy-on-close>
            <FileEdit v-model="obj.drawer" @init="init" @close="obj.drawer.show = false" />
        </el-drawer>
    </div>
</template>


<script setup>
    import { ref,reactive,getCurrentInstance,onMounted,computed } from 'vue'
    import { Back,DocumentDelete,DocumentCopy,More } from '@element-plus/icons-vue'
    import FileEdit from './fileEdit.vue'
    let { proxy } = getCurrentInstance()

    const dropdown1 = ref(null)
    let obj = reactive({
        drawer: {
            show: false,
            title: "",
            data: {}
        },
        req:{
            purpose: 'retrieval',//用于知识库检索，支持 doc、docx、pdf、xlsx 等文件格式，文件限制为 50 M。总容量不超过1G。
            knowledge_id: proxy.$route.query.id,
            page: 1,
            limit: 1000,
            count: 0,
            after: '',
            order: 'desc',
            
        },
        ary:[],
    })

    onMounted(async()=>{
        const token_ = await getToken()
        sessionStorage.setItem("token_", token_)
        setTimeout(() => {
            init()
        }, 0);
    })


    const showtab=(x)=>{
        console.log('proxy.$refs',proxy.$refs[`dropdown${x.id}`]);
        
        if (!proxy.$refs[`dropdown${x.id}`][0]) return
        proxy.$refs[`dropdown${x.id}`][0].handleOpen()
    }


    let getToken = () => {
        return new Promise((resolve, reject) => {
            proxy.$ajax({
                url: '/manager/getToken',
                data: {},
                bfLoading: true
            }).then(d => {
                resolve(d.msg)
            }).catch(e => {
                reject('')
            })
        })
    }

    let init = (t) => {
        obj.ary = []
        proxy.$ajax({
            baseURL: 'https://open.bigmodel.cn/api/paas/v4/files',
            url: '',
            method: 'get',
            data: obj.req,
            isParams: true,
            isResovle: true,
        }).then(d => {
            if(d.list&&d.list.length){
                obj.ary = d.list
                obj.req.count = d.total
            }else{
                obj.ary = []
                obj.req.count = 0
            }
        }).catch(() => { })
    }

    let del = x => {
        proxy.$ajax({
            baseURL: `https://open.bigmodel.cn/api/paas/v4/files/${x.id}`,
            url: '',
            method: 'delete',
            data: {},
            isResovle: true,
        }).then(d => {
            proxy.elMessageMixin("删除成功", 'success')
            init()
        }).catch(() => { })
    }

    let add = () => {
        obj.drawer.show = true
        obj.drawer.title = "上传知识"
        obj.drawer.data = {knowledge_id: obj.req.knowledge_id};
    }

    let preview = x => {
        window.open(x.url)
    }
</script>

<style lang="scss" scoped>
.listDiv{min-width: 856px;}
.backicon{font-size: 22px;font-weight: bold;cursor: pointer;}
.title{font-size: 18px;font-weight: bold;}
.filecard{
    width: calc(25% - 9px);min-width: 200px;height: 120px;border-radius: 10px;border: solid 1px #dcdcdc;cursor: pointer;
    padding: 20px;box-sizing: border-box;margin-right: 12px; margin-bottom: 12px;
    .name{font-size: 16px;}
    .desc{position: relative;font-size: 12px;color: #999;padding-right: 6px;padding-left: 6px;}
    .desc::after{
        content: '';position: absolute;right: 0;top: 50%;margin-top: -6px;width: 1px;height: 12px;background: #888;
    }
    .desc:last-child::after{width: 0 !important;}
}
.filecard:nth-child(4n){margin-right: 0;}
.filecard:hover{background: #f0f0f0;}
</style>